package cn.powertime.evaluation.web.controller;

import cn.powertime.evaluation.entity.CpLionUser;
import cn.powertime.evaluation.entity.SysUser;
import cn.powertime.evaluation.enums.HttpStatus;
import cn.powertime.evaluation.exception.BusinessException;
import cn.powertime.evaluation.facade.SysUserFacade;
import cn.powertime.evaluation.service.CpLionUserService;
import cn.powertime.evaluation.shiro.ShiroUtils;
import cn.powertime.evaluation.vo.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Splitter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author liqi
 * @Description: pc端用户管理模块
 * @date 2019/4/18
 */
@RestController
@RequestMapping(value = "/web/user")
@Api(value = "/web/user", tags = "用户接口", produces = MediaType.ALL_VALUE)
public class UserController extends BaseController {

    @Autowired
    private SysUserFacade sysUserFacade;


    @ApiOperation(value = "验证账号唯一性", notes = "")
    @PostMapping("/checkUserNameOnly")
    public Object checkOnly(String account, Long id) {
        boolean b = sysUserFacade.checkUserNameOnly(account, id);
        if (b) {
            return error(HttpStatus.CHECK_FAIL, null, "账号已存在");
        }
        return success("账号可用");
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册", notes = "")
    public Object add(@Validated @RequestBody ReqUserAddVo addVo) {
        String password = addVo.getPassword();
        if (!StringUtils.equals(password, addVo.getRepeatPassword())) {
            return error("两次输入密码不一致");
        }
        addVo.setRepeatPassword("");
        boolean b = sysUserFacade.add(addVo);
        if (b) {
            return success("用户注册成功");
        }
        return error("用户注册失败");
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改用户信息", notes = "")
    public Object edit(@Validated @RequestBody ReqSysUserEditVo editVo) {
        boolean b = sysUserFacade.edit(editVo);
        if (b) {
            return success("修改用户成功");
        }
        return error("修改用户失败");
    }


    @ApiOperation(value = "修改密码", notes = "")
    @PostMapping(value = "/updatePwd")
    public Object updatePwd(@Validated @RequestBody  ReqSysUserUpdatePwdVo vo) {
        if (!StringUtils.equals(vo.getNewPassword(), vo.getRepPassword())) {
            return error("两次输入密码不一致");
        }
        vo.setRepPassword("");
        boolean b = sysUserFacade.updatePwd(vo);
        if (b) {
            return success("密码修改成功");
        }
        return error("密码修改失败");
    }

//    @ApiOperation(value = "重置密码", notes = "")
//    @PostMapping(value = "/resetPwd")
//    public Object resetPwd(@Validated @RequestBody ReqSysUserResetPwdReqVo vo) {
//        if (!StringUtils.equals(vo.getNewPassword(), vo.getRepPassword())) {
//            throw new BusinessException("两次输入密码不一致");
//        }
//        vo.setRepPassword("");
//        boolean b = sysUserFacade.resetPwd(vo);
//        if (b) {
//            return success("密码重置成功");
//        }
//        return error("密码重置失败");
//    }

    @ApiOperation(value = "用户详情", notes = "")
    @GetMapping(value = "/details/{id}")
    public Object details(@PathVariable("id") @NotNull(message = "字典类型ID不能为空") Long id) {
        SysUser sysUser = sysUserFacade.details(id);
        sysUser.setPassword("");
        return success(sysUser);
    }

    @ApiOperation(value = "用户信息", notes = "")
    @GetMapping(value = "/userInfo")
    public Object userInfo() {
        SysUser sysUser = sysUserFacade.details(ShiroUtils.getUserInfo().getId());
        sysUser.setPassword("");
        return success(sysUser);
    }

    @PostMapping("/update/account")
    @ApiOperation(value = "修改帐号信息",notes ="")
    public Object updateUserInfo(@Validated @RequestBody ReqSysUserUpdateReqVo editVo){
        boolean b = sysUserFacade.accountUpdate(editVo);
        if(b){
            return success("修改账号成功");
        }
        return error("修改账号失败");
    }
}
